package com.loopy.fruns.web;

import com.loopy.fruns.entity.Furn;
import com.loopy.fruns.entity.Page;
import com.loopy.fruns.entity.PageVO;
import com.loopy.fruns.service.FurnService;
import com.loopy.fruns.service.impl.FurnServiceImpl;
import com.loopy.fruns.utils.DataUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

public class FurnServlet extends BasicServlet {
    private FurnService furnService=new FurnServiceImpl();
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Furn> furns = furnService.queryFurns();
//        for (Furn furn : list) {
//            System.out.println(furn);
//        }
        request.setAttribute("furns",furns);
        request.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(request,response);
//        System.out.println("FurnServlet被调用");
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String name = request.getParameter("name");
//        String maker = request.getParameter("maker");
//        String price = request.getParameter("price");
//        String sales = request.getParameter("sales");
//        String stock = request.getParameter("stock");


//        Furn furn = null;
//        try {
//            furn = new Furn(null,name,maker,new BigDecimal(price),
//                    new Integer(sales),new Integer(stock),"assets/images/product-image/6.jpg");
//        } catch (NumberFormatException e) {
//            request.setAttribute("mes","添加数据格式不对");
//            request.getRequestDispatcher("/views/manage/furn_add.jsp").forward(request,response);
//            return;
//        }

//                  Furn furn = new Furn(null,name,maker,new BigDecimal(price),
//                    new Integer(sales),new Integer(stock),"assets/images/product-image/6.jpg");
//        furnService.add(furn);
//       请求转发会造成数据重复提交
//        Furn furn = new Furn();
//        try {
//            BeanUtils.populate(furn,request.getParameterMap());
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
        Furn furn = DataUtils.copyParamToBean(request.getParameterMap(), new Furn());
        furnService.add(furn);
        response.sendRedirect(request.getContextPath()+"/manage/furn?action=list");
    }


    protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = DataUtils.parseInt(req.getParameter("id"), 0);
        int delete = furnService.deleteFurnById(id);
        resp.sendRedirect(req.getContextPath()+"/manage/furn?action=list");
    }


    protected void showFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        Integer id = DataUtils.parseInt(req.getParameter("id"),0);
        Furn furn = furnService.queryFurnById(id);
        req.setAttribute("furn",furn);
        req.getRequestDispatcher("/views/manage/furn_update.jsp").forward(req,resp);
    }


    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Furn furn = DataUtils.copyParamToBean(req.getParameterMap(), new Furn());
        int i = furnService.updateFurn(furn);
//        resp.sendRedirect(req.getContextPath()+"/manage/furn?action=list");
        resp.sendRedirect(req.getContextPath()+"/manage/furn?action=pageFurn&pageNo="+req.getParameter("pageNo"));
    }

    protected void pageFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"),Page.PAGE_SIZE);
        Page<Furn> page = furnService.page(pageNo, pageSize);
        System.out.println(page);
        req.setAttribute("pageFurn",page);
        req.getRequestDispatcher("/views/manage/furn_manage.jsp").forward(req,resp);
    }
}
